/*
   BaseControl.cpp
   
   Copyright (C) 2006 LinuxOne, Inc. <Republic of korea>

   This library is free software; you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as
   published by the Free Software Foundation; either version 2.1
   of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   Lessor General Public License for more details.

   You should have received a copy of the GNU Lessor General Public
   License along with this library; if not, write to the Free Software
   Foundation, inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

   Hyunjin Kim <mgc521@linuxone.co.kr>

   LinuxOne, Inc., hereby disclaims all copylight
   written by Hyunjin Kim, 20 December 2006
*/

#include <BaseControl.h>

namespace booyopos
{

CBaseControl::CBaseControl()
{
	deviceControlDescription="BooyoPOS Control Module";
	deviceControlVersion=0;

	bOpen = false;

	services::CBaseService service;
	bs=&service;
}

CBaseControl::~CBaseControl()
{
}

/* Properties */
std::string
CBaseControl::GetCheckHealthText()
throw (CByException)
{
	try
	{
		if(!bOpen)
		{
			throw CByException(E_CLOSED, "Control not opened");
		}

		return bs->GetCheckHealthText();
	}
	catch(CByException bye)
	{
		throw bye;
	}
	catch(std::exception e)
	{
		throw CByException(E_FAILURE, "Unhandled exception from Device Service", e);
	}
}

bool
CBaseControl::GetClaimed()
throw (CByException)
{
	try
	{
		if(!bOpen)
		{
			throw CByException(E_CLOSED, "Control not opened");
		}

		return bs->GetClaimed();
	}
	catch(CByException bye)
	{
		throw bye;
	}
	catch(std::exception e)
	{
		throw CByException(E_FAILURE, "Unhandled exception from Device Service", e);
	}
}

std::string
CBaseControl::GetDeviceControlDescription()
{
	return deviceControlDescription;
}

int
CBaseControl::GetDeviceControlVersion()
{
	return deviceControlVersion;
}

bool
CBaseControl::GetDeviceEnabled()
throw (CByException)
{
	try
	{
		if(!bOpen)
		{
			throw CByException(E_CLOSED, "Control not opened");
		}

		return bs->GetDeviceEnabled();
	}
	catch(CByException bye)
	{
		throw bye;
	}
	catch(std::exception e)
	{
		throw CByException(E_FAILURE, "Unhandled exception from Device Service", e);
	}
}

void
CBaseControl::SetDeviceEnabled(bool deviceEnabled)
throw (CByException)
{
	try
	{
		if(!bOpen)
		{
			throw CByException(E_CLOSED, "Control not opened");
		}

		bs->SetDeviceEnabled(deviceEnabled);
	}
	catch(CByException bye)
	{
		throw bye;
	}
	catch(std::exception e)
	{
		throw CByException(E_FAILURE, "Unhandled exception from Device Service", e);
	}
}

std::string
CBaseControl::GetDeviceServiceDescription()
throw (CByException)
{
	try
	{
		if(!bOpen)
		{
			throw CByException(E_CLOSED, "Control not opened");
		}

		return bs->GetDeviceServiceDescription();
	}
	catch(CByException bye)
	{
		throw bye;
	}
	catch(std::exception e)
	{
		throw CByException(E_FAILURE, "Unhandled exception from Device Service", e);
	}
}

int
CBaseControl::GetDeviceServiceVersion()
throw (CByException)
{
	try
	{
		if(!bOpen)
		{
			throw CByException(E_CLOSED, "Control not opened");
		}

		return bs->GetDeviceServiceVersion();
	}
	catch(CByException bye)
	{
		throw bye;
	}
	catch(std::exception e)
	{
		throw CByException(E_FAILURE, "Unhandled exception from Device Service", e);
	}
}

bool
CBaseControl::GetFreezeEvents()
throw (CByException)
{
	try
	{
		if(!bOpen)
		{
			throw CByException(E_CLOSED, "Control not opened");
		}

		return bs->GetFreezeEvents();
	}
	catch(CByException bye)
	{
		throw bye;
	}
	catch(std::exception e)
	{
		throw CByException(E_FAILURE, "Unhandled exception from Device Service", e);
	}
}

void
CBaseControl::SetFreezeEvents(bool freezeEvents)
throw (CByException)
{
	try
	{
		if(!bOpen)
		{
			throw CByException(E_CLOSED, "Control not opened");
		}

		bs->SetFreezeEvents(freezeEvents);
	}
	catch(CByException bye)
	{
		throw bye;
	}
	catch(std::exception e)
	{
		throw CByException(E_FAILURE, "Unhandled exception from Device Service", e);
	}
}

std::string
CBaseControl::GetPhysicalDeviceDescription()
throw (CByException)
{
	try
	{
		if(!bOpen)
		{
			throw CByException(E_CLOSED, "Control not opened");
		}

		return bs->GetPhysicalDeviceDescription();
	}
	catch(CByException bye)
	{
		throw bye;
	}
	catch(std::exception e)
	{
		throw CByException(E_FAILURE, "Unhandled exception from Device Service", e);
	}
}

std::string
CBaseControl::GetPhysicalDeviceName()
throw (CByException)
{
	try
	{
		if(!bOpen)
		{
			throw CByException(E_CLOSED, "Control not opened");
		}

		return bs->GetPhysicalDeviceName();
	}
	catch(CByException bye)
	{
		throw bye;
	}
	catch(std::exception e)
	{
		throw CByException(E_FAILURE, "Unhandled exception from Device Service", e);
	}
}

int
CBaseControl::GetState()
{
	int nState = S_CLOSED;

	if(bOpen)
	{
		try
		{
			nState = bs->GetState();
		}
		catch(std::exception e)
		{
		}
	}

	return nState;
}

/* Common Method */
void 
CBaseControl::Claim(int timeout)
throw (CByException)
{
	try
	{
		if(!bOpen)
		{
			throw CByException(E_CLOSED, "Control not opened");
		}

		bs->Claim(timeout);
	}
	catch(CByException bye)
	{
		throw bye;
	}
	catch(std::exception e)
	{
		throw CByException(E_FAILURE, "Unhandled exception from Device Service", e);
	}
}

void 
CBaseControl::Close()
throw (CByException)
{
	try
	{
		if(!bOpen)
		{
			throw CByException(E_CLOSED, "Control not opened");
		}

		bs->Close();
	}
	catch(CByException bye)
	{
		throw bye;
	}
	catch(std::exception e)
	{
		throw CByException(E_FAILURE, "Unhandled exception from Device Service", e);
	}
	bOpen = false;
}

void 
CBaseControl::CheckHealth(int level)
throw (CByException)
{
	try
	{
		if(!bOpen)
		{
			throw CByException(E_CLOSED, "Control not opened");
		}

		bs->CheckHealth(level);
	}
	catch(CByException bye)
	{
		throw bye;
	}
	catch(std::exception e)
	{
		throw CByException(E_FAILURE, "Unhandled exception from Device Service", e);
	}
}

void
CBaseControl::DirectIO(int command, int data, void* object)
throw (CByException)
{
	try
	{
		if(!bOpen)
		{
			throw CByException(E_CLOSED, "Control not opened");
		}

		bs->DirectIO(command, data, object);
	}
	catch(CByException bye)
	{
		throw bye;
	}
	catch(std::exception e)
	{
		throw CByException(E_FAILURE, "Unhandled exception from Device Service", e);
	}
}

void 
CBaseControl::Open(std::string logicalDeviceName)
throw (CByException)
{
	/* TO DO */
	/* 
	   if(bOpen) then
	   1. serviceLoader.findService(logicalDeviceName)
	   2. serviceConnection.connect()
	   3. serviceConnection.getService()
	   4. createEventCallbacks()
	   5. bs->open(logicalDeviceName, callbacks)
	   6. bOpen = true */
	/*
	   if(!bOpen) then
	   1. bs->close()
	   2. serviceConnectioin.disconnect()
	   3. serviceConnect = null */
	try
	{
		if(bOpen)
		{
			throw CByException(E_ILLEGAL, "Device Control already open");
		}

		if(bs->Open(logicalDeviceName, 1))
		{
			throw CByException(E_FAILURE, "Service is not opened");
		}

		bOpen=true;
	}
	catch(CByException bye)
	{
		throw bye;
	}
	catch(std::exception e)
	{
		throw CByException(E_NOSERVICE, "Could not get service instance", e);
	}
}

void 
CBaseControl::Release()
throw (CByException)
{
	try
	{
		if(!bOpen)
		{
			throw CByException(E_CLOSED, "Control not opened");
		}

		bs->Release();
	}
	catch(CByException bye)
	{
		throw bye;
	}
	catch(std::exception e)
	{
		throw CByException(E_FAILURE, "Unhandled exception from Device Service", e);
	}
}

} // namespace booyopos
